//
//  HJNickNameVC.m
//  project
//
//  Created by jc on 2019/5/8.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJNickNameVC.h"

@interface HJNickNameVC ()
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UIButton *finishBtn;

@end

@implementation HJNickNameVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navTitle = @"昵称";
    [self.finishBtn rounded:UIRectCornerAllCorners radius:6];
    self.nameTF.text = [HJUserManager getUserModel].nickName;
    [self.view setupShadowWithView:self.bgView];
    @weakify(self)
    [[self.nameTF.rac_textSignal filter:^BOOL(NSString * _Nullable value) {
        return value.length >=8;
    }]subscribeNext:^(NSString * _Nullable x) {
        @strongify(self)
        self.nameTF.text = [x substringToIndex:8];
    }];
    
    RAC(self.finishBtn,userInteractionEnabled) = [RACSignal combineLatest:@[self.nameTF.rac_textSignal] reduce:^id (NSString * name){
        @strongify(self)
        if (!name.length) {
            [self.finishBtn setBackgroundColor:UIColor.lightGrayColor];
            return @NO;
        }
        [self.finishBtn setBackgroundColor:UIColor.blackColor];
        return @YES;
    }];
    
    [[self.finishBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [HJUserManager updateUserModelWithParams:@{@"nickName":self.nameTF.text} success:^{
            // 更新融云用户名
            [mNotificationCenter postNotificationName:NotificationRCUserInfo object:nil];
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }];
}



@end
